package xiaohu.day6.HJ67;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int targetNum = 24;
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            List<Double> numbers = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                numbers.add(Double.valueOf(in.nextInt()));
            }

            boolean result = canMake24(numbers, targetNum);
            System.out.println(result);
        }
    }

    private static boolean canMake24(List<Double> numbers, int targetNum) {
        if (numbers.size() == 1) {
            return Math.abs(numbers.get(0) - targetNum) < 1e-6;
        }
        //暴力求解
        for (int i = 0; i < numbers.size(); i++) {
            for (int j = i + 1; j < numbers.size(); j++) {
                double num1 = numbers.get(i);
                double num2 = numbers.get(j);

                List<Double> results = new ArrayList<>();
                results.add(num1 + num2);
                results.add(num1 - num2);
                results.add(num2 - num1);
                results.add(num1 * num2);

                if (num2 != 0) results.add(num1 / num2);
                if (num1 != 0) results.add(num2 / num1);

                numbers.remove(j); // 删除较大的索引以避免索引越界
                numbers.remove(i);

                for (Double result : results) {
                    numbers.add(result);
                    if (canMake24(numbers, targetNum)) {
                        return true;
                    }
                    numbers.remove(numbers.size() - 1); // 移除添加的元素以便下一次迭代
                }

                numbers.add(i, num1); // 恢复原始状态
                numbers.add(j, num2);
            }
        }
        return false;
    }
}
